home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rjs.lha / RJS / NDR / src / NDR_receive.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-14  |  5.9 KB  |  241 lines

  1. #include <string.h>
  2. #include "NDR.h"
  3. #include <libc.h>
  4.  
  5. extern "C" {
  6.     int ftoi(float *);
  7.     int itof(float *);
  8.     int dtoi(double *);
  9.     int itod(double *);
  10.     unsigned short ntohs(unsigned short);
  11. };
  12.  
  13. #if defined(vax) || defined(mips)
  14.     RJS_NDR::NDR_endian        RJS_NDR::ndr_endian=RJS_NDR::NdrLittleEndian;
  15.     RJS_NDR::NDR_character    RJS_NDR::ndr_char=RJS_NDR::NdrASCII;
  16. #endif
  17.  
  18. #if defined(vax)
  19.     RJS_NDR::NDR_floating RJS_NDR::ndr_float=RJS_NDR::NdrVAX;
  20. #elif defined(mips)
  21.     RJS_NDR::NDR_floating RJS_NDR::ndr_float=RJS_NDR::NdrIEEE;
  22. #endif
  23.  
  24.  
  25. RJS_Transport &operator>>(RJS_Transport &tr, RJS_NDR_receive &ndr)
  26. {
  27.     unsigned short length;
  28.     tr.read((char *)&length,sizeof(unsigned short));
  29.     length=ntohs(length);
  30.     // check for errors
  31.     tr.read(ndr.raw_data(),length);
  32.     ndr.init(ndr.raw_data(),length);
  33.     return tr;
  34. }
  35.  
  36. ostream &operator<<(ostream &os, RJS_NDR_receive &ndr)
  37. {
  38.     cout << "local_endian() => " << ndr.local_endian() << endl;
  39.     cout << "local_character() => " << ndr.local_character() << endl;
  40.     cout << "local_floating() => " << ndr.local_floating() << endl;
  41.     cout << "remote_endian() => " << ndr.remote_endian() << endl;
  42.     cout << "remote_character() => " << ndr.remote_character() << endl;
  43.     cout << "remote_floating() => " << ndr.remote_floating() << endl;
  44.     cout << "raw_length() => " << ndr.raw_length() << endl;
  45.     for (int i=0; i< ndr.raw_length(); i++) {
  46.         cout << i << "\t" << int(*(ndr.raw_data()+i)) << endl;
  47.     }
  48.     return os;
  49. }
  50.  
  51. RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr, RJS_XNDR &x) {
  52.     x.to_internal(ndr);
  53.     return ndr;
  54. }
  55.  
  56. void RJS_NDR_receive::reset() 
  57. {
  58.     outptr=start_data+2;
  59. }
  60.  
  61. void RJS_NDR_receive::init(char *buffer,int size)
  62. {
  63.     int align=((unsigned int)(buffer)) % sizeof(double);
  64.     if (align) buffer += (sizeof(double)-align);    
  65.     start_data    =    buffer;
  66.     outptr        =    buffer;
  67.     end_data    =    buffer+size;
  68.     rem_endian    =    RJS_NDR::NDR_endian((*outptr >> 4) & 0x0f);
  69.     rem_char    =    RJS_NDR::NDR_character(*outptr++ & 0x0f);
  70.     rem_float    =    RJS_NDR::NDR_floating(*outptr++);
  71. }
  72.  
  73. RJS_NDR_receive &RJS_NDR_receive::extract(char *d,int &count)
  74. {
  75.     (*this) >> count;
  76.     strncpy(d,outptr,count);
  77.     outptr += count;
  78.     return *this;
  79. }
  80.  
  81.  
  82. RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,char &ch)
  83. {
  84.     //if (passed end_data) error
  85.     ch = *ndr.outptr++;
  86.     return ndr;
  87. }
  88.  
  89. RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,unsigned char &ch)
  90. {
  91.     //if (passed end_data) error
  92.     ch = *ndr.outptr++;
  93.     return ndr;
  94. }
  95.  
  96.  
  97. RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,short &i)
  98. {
  99.     if (((unsigned int)(ndr.outptr)) % sizeof(short)) ndr.outptr++; 
  100.     if (ndr.local_endian()==ndr.remote_endian())
  101.         i = * (short *) ndr.outptr;
  102.     else {
  103.         char *sp= (char *)&i;
  104.         sp[0] = ndr.outptr[1];
  105.         sp[1] = ndr.outptr[0];
  106.     }
  107.     ndr.outptr += sizeof(short);
  108.     return ndr;
  109. }
  110.  
  111. RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,unsigned short &i)
  112. {
  113.     if (((unsigned int)(ndr.outptr)) % sizeof(unsigned short)) ndr.outptr++; 
  114.     if (ndr.local_endian()==ndr.remote_endian())
  115.         i = * (unsigned short *) ndr.outptr;
  116.     else {
  117.         char *sp= (char *)&i;
  118.         sp[0] = ndr.outptr[1];
  119.         sp[1] = ndr.outptr[0];
  120.     }
  121.     ndr.outptr += sizeof(unsigned short);
  122.     return ndr;
  123. }
  124.  
  125. RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,int &i)
  126. {
  127.     int align=((unsigned int)(ndr.outptr)) % sizeof(int);
  128.     if (align) ndr.outptr += (sizeof(int)-align);
  129.     if (ndr.local_endian()==ndr.remote_endian())
  130.         i = * (int *) ndr.outptr;
  131.     else {
  132.         char *sp= (char *)&i;
  133.         sp[0] = ndr.outptr[3];
  134.         sp[1] = ndr.outptr[2];
  135.         sp[2] = ndr.outptr[1];
  136.         sp[3] = ndr.outptr[0];
  137.     }
  138.     ndr.outptr += sizeof(int);
  139.     return ndr;
  140. }
  141.  
  142.  
  143. RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,unsigned int &i)
  144. {
  145.     int align=((unsigned int)(ndr.outptr)) % sizeof(unsigned int);
  146.     if (align) ndr.outptr += (sizeof(unsigned int)-align);
  147.     if (ndr.local_endian()==ndr.remote_endian())
  148.         i = * (unsigned int *) ndr.outptr;
  149.     else {
  150.         char *sp= (char *)&i;
  151.         sp[0] = ndr.outptr[3];
  152.         sp[1] = ndr.outptr[2];
  153.         sp[2] = ndr.outptr[1];
  154.         sp[3] = ndr.outptr[0];
  155.     }
  156.     ndr.outptr += sizeof(unsigned int);
  157.     return ndr;
  158. }
  159.  
  160. RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,float &f)
  161. {
  162.     char buf[8];
  163.     int align=((unsigned int)(ndr.outptr)) % sizeof(float);
  164.     if (align) ndr.outptr += (sizeof(float)-align);
  165.     if (ndr.local_endian()==ndr.remote_endian()) {
  166.         buf[0] = ndr.outptr[0];
  167.         buf[1] = ndr.outptr[1];
  168.         buf[2] = ndr.outptr[2];
  169.         buf[3] = ndr.outptr[3];
  170.     } else {
  171.         buf[0] = ndr.outptr[3];
  172.         buf[1] = ndr.outptr[2];
  173.         buf[2] = ndr.outptr[1];
  174.         buf[3] = ndr.outptr[0];
  175.     }
  176.     if (ndr.local_floating()!=ndr.remote_floating()) {
  177.         if (ndr.local_floating()==RJS_NDR::NdrVAX) {
  178.             if (ndr.remote_floating()==RJS_NDR::NdrIEEE) 
  179.                 itof((float *) &buf);
  180.         } else if (ndr.local_floating()==RJS_NDR::NdrIEEE) {
  181.             if (ndr.remote_floating()==RJS_NDR::NdrVAX) 
  182.                 ftoi((float *) &buf);
  183.         }
  184.     }
  185.     f = * (float *) buf;
  186.     ndr.outptr += sizeof(float);
  187.     return ndr;
  188. }
  189.  
  190.  
  191. RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,double &d)
  192. {
  193.     char buf[8];
  194.     int align=((unsigned int)(ndr.outptr)) % sizeof(double);
  195.     if (align) ndr.outptr += (sizeof(double)-align);
  196.     if (ndr.local_endian()==ndr.remote_endian()) {
  197.          buf[0] = ndr.outptr[0];
  198.         buf[1] = ndr.outptr[1];
  199.         buf[2] = ndr.outptr[2];
  200.         buf[3] = ndr.outptr[3];
  201.         buf[4] = ndr.outptr[4];
  202.         buf[5] = ndr.outptr[5];
  203.         buf[6] = ndr.outptr[6];
  204.         buf[7] = ndr.outptr[7];
  205.     } else {
  206.          buf[0] = ndr.outptr[7];
  207.         buf[1] = ndr.outptr[6];
  208.         buf[2] = ndr.outptr[5];
  209.         buf[3] = ndr.outptr[4];
  210.         buf[4] = ndr.outptr[3];
  211.         buf[5] = ndr.outptr[2];
  212.         buf[6] = ndr.outptr[1];
  213.         buf[7] = ndr.outptr[0];
  214.     }
  215.     if (ndr.local_floating()!=ndr.remote_floating()) {
  216.         if (ndr.local_floating()==RJS_NDR::NdrVAX) {
  217.             if (ndr.remote_floating()==RJS_NDR::NdrIEEE) 
  218.                 itod((double *) &buf);
  219.         } else if (ndr.local_floating()==RJS_NDR::NdrIEEE) {
  220.             if (ndr.remote_floating()==RJS_NDR::NdrVAX) 
  221.                 dtoi((double *) &buf);
  222.         }
  223.     }
  224.  
  225.     d = * (double *) buf;
  226.  
  227.     ndr.outptr += sizeof(double);
  228.     return ndr;
  229. }
  230.  
  231. RJS_NDR_receive &operator>>(RJS_NDR_receive &ndr,char *str)
  232. {
  233.     unsigned short len;
  234.     ndr >> len;
  235.     strncpy(str,ndr.outptr,len);
  236.     ndr.outptr += len;
  237.     return ndr;
  238. }
  239.  
  240.  
  241.